package com.qf.service.impl;

/*

import com.qf.service.IndexService;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;*/

import java.io.IOException;


import com.qf.service.IndexService;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/*
 * @Data: 2021/7/22 20:23
 * */
@Service
public class IndexServiceImpl implements IndexService {

    @Value("${elasticsearch.index.name}")
    private String index;


    private RestHighLevelClient restHighLevelClient;
    @Autowired
    public void setRestHighLevelClient(RestHighLevelClient restHighLevelClient) {
        this.restHighLevelClient = restHighLevelClient;
    }

    @Override
    public void createIndex() throws IOException {
        CreateIndexRequest createIndexRequest=new CreateIndexRequest(index);
//        设置分片数
        createIndexRequest.settings(Settings.builder().put("number_of_shards",3).put("number_of_replicas",2).build());


        XContentBuilder xContentBuilder = JsonXContent.contentBuilder()
                .startObject()
                .startObject("properties")
                .startObject("clientID").field("type", "integer").field("index", "true").endObject()
                .startObject("srcNumber").field("type", "keyword").field("index", "true").endObject()
                .startObject("messagePriority").field("type", "short").field("index", "true").endObject()
                .startObject("srcSequenceId").field("type", "long").field("index", "true").endObject()
                .startObject("gatewayID").field("type", "integer").field("index", "true").endObject()
                .startObject("productID").field("type", "integer").field("index", "true").endObject()
                .startObject("destMobile").field("type", "keyword").field("index", "true").endObject()
                .startObject("messageContent").field("type", "text").field("analyzer", "ik_max_word").field("index", "true").endObject()
                .startObject("description").field("type", "text").field("analyzer", "ik_max_word").field("index", "true").endObject()
                .startObject("msgid").field("type", "keyword").field("index", "true").endObject()
                .startObject("reportState").field("type", "integer").field("index", "true").endObject()
                .startObject("errorCode").field("type", "text").field("index", "true").endObject()
                .startObject("operatorId").field("type", "integer").field("index", "true").endObject()
                .startObject("provinceId").field("type", "integer").field("index", "true").endObject()
                .startObject("cityId").field("type", "integer").field("index", "true").endObject()
                .startObject("source").field("type", "integer").field("index", "true").endObject()
                .startObject("sendTime").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss").field("index", "true").endObject()
                .startObject("updateTime").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss").field("index", "true").endObject()
                .endObject()
                .endObject();
        createIndexRequest.mapping(xContentBuilder);
        restHighLevelClient.indices().create(createIndexRequest,RequestOptions.DEFAULT);

    }
}
